package com.fyzk.activity.gl;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.gl.RandomSetView.RandomBtnClickListener;
import com.fyzk.activity.lottery.shuzicai.BallInfo;
import com.fyzk.activity.lottery.shuzicai.SelectBallView;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.betzhushu.ssq.SSQ_ZhuShu_GL;
import com.tyjc.lottery.R;
import com.fyzk.utils.HandlerTypeUtils;
import com.fyzk.utils.RandomUtil;

import java.util.ArrayList;
import java.util.List;
//import http.ProtocalEngine.ProtocalEngine;
//import http.ProtocalEngine.ProtocalProcess.Default.CommonResult;
//import http.ProtocalEngine.ProtocalProcess.GL.GLRequestData;
//import http.ProtocalEngine.ProtocalProcess.GL.GLResponseData;
//import http.ProtocalEngine.ProtocalProcess.LotteryList.LotteryListRequestData;
//import http.ProtocalEngine.ProtocalProcess.LotteryList.LotteryListResponseData;

/**
 * 双色球过滤类
 * 
 * @author miao
 * 
 */
public class SSQGLActivity extends BaseActivity implements
		RandomBtnClickListener, BallItemClickLitener {

	private CenterLeftTopView mTopView;
	protected SelectBallView redBallView;
	protected SelectBallView blueBallView;

	protected RandomSetView redRandomSet;
	protected RandomSetView blueRandomSet;

	protected List<BallInfo> redBallInfos = new ArrayList<BallInfo>();
	protected List<BallInfo> blueBallInfos = new ArrayList<BallInfo>();

	protected List<List<BallInfo>> arrs = new ArrayList<List<BallInfo>>();

	private TextView mTxt_amount;// 注码彩金
	private RadioGroup mRdg_fs;
	private RadioButton mRdg_z6b5, mRdg_z6b4, mRdg_z5b5, mRdg_z5b4;

	private final int Z6B5 = 2;
	private final int Z6B4 = 1;
	private final int Z5B5 = 4;
	private final int Z5B4 = 3;
	private int type_fs = Z6B5;//

	long zhushu;

	private String red_code = "";
	private String blue_code = "";

	private ProgressDialog progressdialog;

	private int minRed = 8;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_juzhengl);
		TouZhuHelper.getInstance().setBallItemClickLitener(this);
		mTopView = (CenterLeftTopView) findViewById(R.id.rlTitle);
		mTopView.setOnTopTitleClickLitener(this);
		mTopView.setRightText("开始过滤");
		redBallView = (SelectBallView) findViewById(R.id.redball);
		blueBallView = (SelectBallView) findViewById(R.id.blueball);
		initRandomSetView();
		initBallList();
		arrs.add(redBallInfos);
		arrs.add(blueBallInfos);
		redBallView.setBallInfoList(redBallInfos);
		blueBallView.setBallInfoList(blueBallInfos);
		initBallViewMax();
		mTxt_amount = (TextView) findViewById(R.id.select_amount);
		mRdg_z6b5 = (RadioButton) findViewById(R.id.gl_rdb_z6b5);
		mRdg_z6b4 = (RadioButton) findViewById(R.id.gl_rdb_z6b4);
		mRdg_z5b5 = (RadioButton) findViewById(R.id.gl_rdb_z5b5);
		mRdg_z5b4 = (RadioButton) findViewById(R.id.gl_rdb_z5b4);
		mRdg_fs = (RadioGroup) findViewById(R.id.gl_rdg);
		mRdg_fs.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.gl_rdb_z6b5:// 中6包5
					type_fs = Z6B5;
					redRandomSet.setMinRandomNum(8, 33);
					redBallView.setSelect_max(33);
					minRed = 8;
					redRandomSet.setLineTitle("红球（8~33）");
					break;
				case R.id.gl_rdb_z6b4:// 中6保4
					type_fs = Z6B4;
					redRandomSet.setMinRandomNum(8, 32);
					redBallView.setSelect_max(32);
					minRed = 8;
					redRandomSet.setLineTitle("红球（8~32）");
					break;
				case R.id.gl_rdb_z5b5:// 中5保5
					type_fs = Z5B5;
					redRandomSet.setMinRandomNum(8, 31);
					redBallView.setSelect_max(31);
					minRed = 8;
					redRandomSet.setLineTitle("红球（8~31）");
					break;
				case R.id.gl_rdb_z5b4:// 中5保4
					type_fs = Z5B4;
					redRandomSet.setMinRandomNum(8, 33);
					redBallView.setSelect_max(33);
					minRed = 8;
					redRandomSet.setLineTitle("红球（8~33）");
					break;
				case R.id.gl_rdb_nil:
					type_fs = 0;
					break;
				}
			}
		});
//		request(SchemaDef.LOTTERY_LIST);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 是否可以过滤
	 * 
	 * @return
	 */
	public boolean glAble() {
		return zhushu > 0 && getRedSelectNum() >= minRed;
	}

//	public static GLResponseData result;
//	LotteryListResponseData lotterylistdata;

//	@Override
//	public void onProtocalSuccess(Object obj) {
//		super.onProtocalSuccess(obj);
//		if (obj instanceof GLResponseData) {
//			result = (GLResponseData) obj;
//			CommonResult commonResult = result.commonResult;
//			if (commonResult.codeValue == 0) {
//				// this.mArr_HistoryItems = result.historyItems;
//				mHandler.sendEmptyMessage(HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA);
//			} else {
//				Message msg = mHandler.obtainMessage();
//				msg.obj = commonResult.tips;
//				msg.what = HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL;
//				mHandler.sendMessage(msg);
//			}
//		} else if (obj instanceof LotteryListResponseData) {
//			lotterylistdata = (LotteryListResponseData) obj;
//			int codeValue = lotterylistdata.commonResult.codeValue;
//			if (codeValue == 0) {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_SUCCESS);
//			} else {
//				mHandler.sendEmptyMessage(HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL);
//			}
//		}
//
//	}

	@Override
	protected void parserMessage(Message msg) {
		super.parserMessage(msg);
		if (progressdialog != null) {
			progressdialog.dismiss();
		}
		switch (msg.what) {
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA:
			Intent intent = new Intent(SSQGLActivity.this,
					SSQGLResultActivity.class);
			// intent.putExtra("result", msg.obj.toString());
			startActivity(intent);
			break;
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL:
			AppToast.getToast().show("联网失败");
			break;
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_SUCCESS:// 获取期号成功
			// lotteryInfos

//			LotteryInfo info = lotterylistdata.lotteryInfos.get(0);
//			AppLotteryInfos.setEndTime(LotteryIDUtils.SSQ_LOTTERY_ID_INT,
//					info.nextPeriodTime);
//			AppLotteryInfos.setPeriod(LotteryIDUtils.SSQ_LOTTERY_ID_INT,
//					info.period);
//			AppLotteryInfos.setSellType(LotteryIDUtils.SSQ_LOTTERY_ID_INT,
//					info.sellType);
//
//			if (info.sellType.equals("2")) {
//				AppToast.getToast().show("当前彩种已停售");
//			}

			break;
		default:
			break;
		}
	}
	
	

	private void initCode() {
		StringBuffer red = new StringBuffer();
		StringBuffer blue = new StringBuffer();
		for (int i = 0; i < arrs.get(0).size(); i++) {
			BallInfo info = arrs.get(0).get(i);
			if (info.isSelected()) {
				red.append(info.getNum()).append(",");
			}
		}
		if (red.length() > 1) {
			red.deleteCharAt(red.length() - 1);
			red_code = red.toString();
		}
		for (int i = 0; i < arrs.get(1).size(); i++) {
			BallInfo info = arrs.get(1).get(i);
			if (info.isSelected()) {
				blue.append(info.getNum()).append(",");
			}
		}
		if (blue.length() > 1) {
			blue.deleteCharAt(blue.length() - 1);
			blue_code = blue.toString();
		}
	}

	/**
	 * 网络连接提示框
	 */
	protected ProgressDialog CreateDialog() {
		String waitMessage = "数据处理中……";
		progressdialog = new ProgressDialog(this);
		progressdialog.setMessage(waitMessage);
		progressdialog.setIndeterminate(true);
		progressdialog.setCancelable(true);
		return progressdialog;
	}

	@Override
	public void onRightClick() {// 矩阵过滤
		super.onRightClick();
		if (glAble()) {
			initCode();
			CreateDialog().show();
//			request(SchemaDef.GL);
		} else {
			AppToast.getToast().show("红球个数至少为8个，篮球至少选择1个！");
		}
	}

	public void initRandomSetView() {
		blueRandomSet = (RandomSetView) findViewById(R.id.randomSet_blue);
		redRandomSet = (RandomSetView) findViewById(R.id.randomSet_red);
		redRandomSet.setOnRandomBtnClickListener(this);
		blueRandomSet.setOnRandomBtnClickListener(this);
	}

	public void initBallViewMax() {
		redBallView.setSelect_max(17);
	}

	public int getRedSelectNum() {
		int selectNum = 0;
		for (BallInfo info : redBallInfos) {
			if (info.isSelected()) {
				selectNum++;
			}
		}
		return selectNum;
	}

	/**
	 * 初始化 小球list
	 * 
	 * @param balls
	 * @param count
	 *            每列显示小球数
	 * @param start
	 */
	public void initBallList(List<BallInfo> balls, int count, int start,
			boolean hasZero) {
		balls.clear();
		for (int j = start; j < count + start; j++) {
			BallInfo qiu;
			if (hasZero) {
				if (j < 10) {
					qiu = new BallInfo("0" + j);
				} else {
					qiu = new BallInfo("" + j);
				}
			} else {
				qiu = new BallInfo(String.valueOf(j));
			}
			balls.add(qiu);
		}
	}

	public void initBallList() {
		initBallList(redBallInfos, 33, 1, true);
		initBallList(blueBallInfos, 16, 1, true);
	}

//	@Override
//	protected void request(int schemaDef) {
//		super.request(schemaDef);
//		ProtocalEngine engine;
//		switch (schemaDef) {
//		case SchemaDef.GL:
//			engine = new ProtocalEngine(getContext());
//			GLRequestData request = new GLRequestData();
//			request.play = String.valueOf(type_fs);
//			request.lotteryType = "1001";
//			request.codeValue = red_code;
//			request.appendcode = blue_code;
//			engine.request(this, schemaDef, request);
//			break;
//		case SchemaDef.LOTTERY_LIST:
//			engine = new ProtocalEngine(getContext());
//			LotteryListRequestData requestData = new LotteryListRequestData();
//			requestData.lotteryType = LotteryIDUtils.SSQ_LOTTERY_ID_INT;
//			engine.request(this, schemaDef, requestData);
//		}
//	}
//
//
//	@Override
//	public void onProtocalError(Object paramObject) {
//		super.onProtocalError(paramObject);
//		if (progressdialog != null) {
//			progressdialog.dismiss();
//		}
//	}

	@Override
	public void onRandomBtnClick(RandomSetView v, int randomCount) {
		switch (v.getId()) {
		case R.id.randomSet_red:
			int redArr[] = RandomUtil.randomNoRepeat(randomCount, 0, 33);
			initBallList(redBallInfos, 33, 1, true);
			for (int i = 0; i < redArr.length; i++) {
				redBallInfos.get(redArr[i]).setSelected(true);
			}
			redBallView.invalidate();
			break;
		case R.id.randomSet_blue:
			int blueArr[] = RandomUtil.randomNoRepeat(randomCount, 0, 16);
			initBallList(blueBallInfos, 16, 1, true);
			for (int i = 0; i < blueArr.length; i++) {
				blueBallInfos.get(blueArr[i]).setSelected(true);
			}
			blueBallView.invalidate();
			break;

		default:
			break;
		}
		ballClick(null);
	}

	@Override
	public void ballClick(WeiZhi wz) {
		zhushu = new SSQ_ZhuShu_GL().betZhuShu(arrs);
		mTxt_amount.setText("总计，" + zhushu + "注 " + zhushu * 2 + "米");
	}

}
